package com.lin.controller;

import com.lin.VO.ResultVO;
import com.lin.dataobject.Category;
import com.lin.enums.ResultEnum;
import com.lin.exception.SellException;
import com.lin.form.CategoryForm;
import com.lin.server.CategoryService;
import com.lin.utils.ResultVOUtils;
import com.lin.utils.StringUtils;
import com.lin.utils.treeuntil.CategoryTreeUntil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/9.
 */
@Controller
@RequestMapping("/allCategory")
public class SellerAllCategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/treeView")
    public ModelAndView treeView(){
        return new ModelAndView("category/index2");
    }

//    @RequestMapping("/tree")
//    @ResponseBody
//    public List<Category> getTreeData(){
//        List<Category> list=null;
//        try{
//            List<Category> institutions = categoryService.findAll();
//            CategoryTreeUntil treeUntil=new CategoryTreeUntil();
//            list=treeUntil.fillTree(institutions);
//
//        }catch(Exception e) {
//            e.printStackTrace();
//        }
//        return list;
//    }

    @RequestMapping("/view")
    public ModelAndView view(@RequestParam(value = "id",required = false) String id,
                             Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            Category category= categoryService.findById(Integer.valueOf(id));
            map.put("category",category);
        }
        return new ModelAndView("category/add_view");
    }

    @RequestMapping("save")
    @ResponseBody
    public ResultVO save(@Valid CategoryForm form, BindingResult result){
        if (result.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),
                    result.getFieldError().getDefaultMessage());
        }
        Category category=new Category();
        try{
            if (StringUtils.isNotEmpty(form.getId())){
                category= categoryService.findById(form.getId());
            }
            if (!StringUtils.isNotEmpty(form.getPid())){
                form.setPid(0);
            }
            BeanUtils.copyProperties(form,category);
            if (category.getPid()!=0) {
                category.setTopId(categoryService.getTopId(category).getId());
            }
            categoryService.save(category);
        }catch (SellException e){
            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
                    ResultEnum.SAVE_ERROR.getMessage());
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @PostMapping("del")
    public ResultVO del(@RequestParam(value = "id",required = false) Integer id){
        Category category= categoryService.findById(id);
        if (category==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        Long childrenCount= categoryService.getChildrenCount(category.getId());
        System.out.println(childrenCount);
        if (childrenCount>0){
            return ResultVOUtils.error(ResultEnum.HAVE_CHILDREN.getCode(),
                    ResultEnum.HAVE_CHILDREN.getMessage());
        }
        categoryService.del(category);
        return ResultVOUtils.success("删除成功");
    }

    @RequestMapping("/getAllData")
    @ResponseBody
    public List<Category> getAllData(){
        List<Category> list=categoryService.findAll();
        return list;
    }
}
